/*
  剪裁
  如果用于头像显示，可以用CircleAvatar控件
 */
import 'package:flutter/material.dart';

void main() => runApp(MaterialApp(
  home: HomePage(),
));

class HomePage extends StatelessWidget {

  final imgName = 'mg.jpg';

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        children: [
          // 圆形
          SizedBox(
            width: 100,
            height: 100,
            child: ClipOval(
              child: Image.asset(imgName),
            ),
          ),
          // 圆角矩形
          SizedBox(
            width: 100,
            height: 100,
            child: ClipRRect(
              borderRadius: BorderRadius.all(Radius.circular(8.0)),
              child: Image.asset(imgName),
            ),
          ),
          // 自定义区域
          ClipOval(
            clipper: MyClipper(),
            child: Image.asset(imgName),
          ),
          // 头像控件
          CircleAvatar(
            radius: 20,
            child: Image.asset(imgName),
          ),
          // 自定义路径
          ClipPath(
            child: Image.asset(imgName),
            clipper: PathClipper(),
          )
        ],
      ),
    );
  }
}

// 返回一个Path对象即可
class PathClipper extends CustomClipper<Path> {
  @override
  Path getClip(Size size) {
    return getLines(size);
  }

  /*
    直线
   */
  Path getLines(Size size) {
    var path = Path();
    path.relativeLineTo(0, 0);
    path.relativeLineTo(size.width/2, 0);
    path.relativeLineTo(size.width, size.height);
    path.relativeLineTo(0, size.height);
    path.close();
    return path;
  }

  /*
    贝塞尔曲线
   */
  Path getBezier(Size size) {
    var path = Path();
    // 以后有需要再用
    return path;
  }

  @override
  bool shouldReclip(covariant CustomClipper<Path> oldClipper) {
    return false;
  }
}


/*
  自定义剪裁大小和位置
 */
class MyClipper extends CustomClipper<Rect> {
  @override
  Rect getClip(Size size){
    // return Rect.fromLTWH(0, 0, 100.0, 100.0);
    return Rect.fromCenter(center: Offset(size.width/2, size.height/2), width: 100, height: 100);
  }

  @override
  bool shouldReclip(CustomClipper<Rect> oldClipper) => false;
}